package com.lmm.lcbusiness.lcbusiness.pages;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.alibaba.android.arouter.launcher.ARouter;
import com.igexin.sdk.PushManager;
import com.lmm.dresswisdom.lmmframe.components.alert.AlertUtils;
import com.lmm.dresswisdom.lmmframe.util.LogUtils;
import com.lmm.lcbusiness.lcbusiness.AppBaseActivity;
import com.lmm.lcbusiness.lcbusiness.R;
import com.lmm.lcbusiness.lcbusiness.server.DemoIntentService;
import com.lmm.lcbusiness.lcbusiness.server.DemoPushService;
import com.lmm.lcbusiness.lcbusiness.server.UpdateIntentService;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;

/**
 * @author xmgong
 */

public class InitActivity extends AppBaseActivity implements View.OnClickListener {

    String[] persmissions = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
    };


    ScheduledExecutorService service = null;

    int times = 3;
    Handler mhandler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            if (msg.what == 1) {
                LogUtils.write("************************");
                if (times > 0) {
                    btnNext.setText("跳过(" + times-- + ")");
                } else {
                    toMain();
                }
            }
        }
    };

    Button btnNext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_init);
        hideActionBar();
        btnNext = findViewById(R.id.btn_next);
        btnNext.setOnClickListener(this);

        service = Executors.newScheduledThreadPool(1);

        PermissionDialog();
        startUpdateService();
    }

    public void toMain() {
        ARouter.getInstance().build("/app/rnactivity").navigation();
        finish();
    }

    private void startUpdateService() {
        Intent intent = new Intent(this, UpdateIntentService.class);
        startService(intent);
    }

    //权限提示
    private void PermissionDialog() {
        PermissionGen.needPermission(this, 100, persmissions);
    }

    @PermissionSuccess(requestCode = 100)
    private void doSuccess() {
        LogUtils.write("PermissionSuccess");
        start();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        for (String s : permissions) {
            LogUtils.write(s);
        }
        for (int i : grantResults) {
            LogUtils.write("grantResults:" + i);
        }
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @PermissionFail(requestCode = 100)
    private void diFail() {
        AlertUtils.remindAlert(this, "提醒", "授权失败，部分功能将无法正常使用!", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                toMain();
            }
        });
    }


    private void start() {
        service.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                mhandler.sendEmptyMessage(1);
            }
        }, 1, 1, TimeUnit.SECONDS);
    }

    @Override
    public void onClick(View v) {
        if (v == btnNext) {
            toMain();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        service.shutdown();
    }
}

